from django.contrib import admin
from .models import User

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = ('username', 'role', 'is_active')  # 列表页显示用户名、角色、是否活跃
    fields = ('username', 'password', 'role', 'is_active', 'is_staff')  # 编辑页显示的字段（包含角色）
    search_fields = ('username',)
    list_filter = ('role', 'is_active')
    readonly_fields = ('password',)  # 密码字段只读

    def get_readonly_fields(self, request, obj=None):
        # 新增用户时密码可写，编辑时只读
        if obj:
            return self.readonly_fields
        return ()

    list_per_page = 20  # 每页显示20条